package com.wyh.springbootrabbitmq.config;


import com.wyh.springbootrabbitmq.constant.Constant;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    //工作队列模式
    @Bean("workQueue")
    public Queue workQueue(){
        return QueueBuilder.durable(Constant.WORK_QUEUE).build();
    }

    //发布订阅模式
    @Bean("fanoutQueue1")
    public Queue fanoutQueue1(){
        return QueueBuilder.durable(Constant.FANOUT_QUEUE1).build();
    }

    @Bean("fanoutQueue2")
    public Queue fanoutQueue2(){
        return QueueBuilder.durable(Constant.FANOUT_QUEUE2).build();
    }

    @Bean("fanoutExchange")
    public FanoutExchange fanoutExchange(){
        return ExchangeBuilder.fanoutExchange(Constant.FANOUT_EXCHANGE).build();
    }

    @Bean("fanoutQueueBinding1")
    public Binding bind1(@Qualifier("fanoutExchange") FanoutExchange exchange,
                         @Qualifier("fanoutQueue1") Queue queue){
        return BindingBuilder.bind(queue).to(exchange);
    }

    @Bean("fanoutQueueBinding2")
    public Binding bind2(@Qualifier("fanoutExchange") FanoutExchange exchange,
                         @Qualifier("fanoutQueue2") Queue queue){
        return BindingBuilder.bind(queue).to(exchange);
    }

    /**
     * 路由模式
     * 1.声明交换机
     * 2.声明队列
     * 3.交换机与队列绑定
     */
    @Bean("directExchange")
    public DirectExchange directExchange(){
        return ExchangeBuilder.directExchange(Constant.DIRECT_EXCHANGE).build();
    }

    @Bean("directQueue1")
    public Queue directQueue1(){
        return QueueBuilder.durable(Constant.DIRECT_QUEUE1).build();
    }

    @Bean("directQueue2")
    public Queue directQueue2(){
        return QueueBuilder.durable(Constant.DIRECT_QUEUE2).build();
    }

    @Bean("directQueueBinding1")
    public Binding directQueueBinding1(@Qualifier("directExchange") DirectExchange directExchange,
                                       @Qualifier("directQueue1") Queue queue){
        return BindingBuilder.bind(queue).to(directExchange).with("a");
    }

    @Bean("directQueueBinding2")
    public Binding directQueueBinding2(@Qualifier("directExchange") DirectExchange directExchange,
                                       @Qualifier("directQueue2") Queue queue){
        return BindingBuilder.bind(queue).to(directExchange).with("a");
    }

    @Bean("directQueueBinding3")
    public Binding directQueueBinding3(@Qualifier("directExchange") DirectExchange directExchange,
                                       @Qualifier("directQueue2") Queue queue){
        return BindingBuilder.bind(queue).to(directExchange).with("b");
    }

    /**
     * 通配符模式
     *
     */
}
